<?php

/**
*/

define('IN_ECS', true);

require('./init.php');
require_once('../includes/cls_json.php');

$nearID = $_REQUEST['nearID'];
if (is_blank($nearID)) {
	exit(0);
}

$json = new JSON;

$res = array('error' => 0);

$sql = " DELETE FROM " . $ecs->table('vcustomer_login') . " WHERE " . time() . " - last_time > 120 OR token_id = '$nearID'";
$db->query($sql);
$sql = " DELETE FROM " . $ecs->table('vsalesperson_login') . " WHERE " . time() . " - last_time > 120 OR token_id = '$nearID'";
$db->query($sql);

$sql = " UPDATE " . $ecs->table('vsalesperson_login') .
	" SET customer_id = NULL, customer_token = NULL " .
	" WHERE customer_id is NOT NULL AND customer_id not in ( " .
	"     SELECT id " .
	"       FROM " . $ecs->table('vcustomer_login') .
	" )";
$db->query($sql);
$sql = " DELETE FROM " . $ecs->table('vwaiting_queue') . 
	" WHERE customer_id not in ( " .
	"     SELECT id " .
	"       FROM " . $ecs->table('vcustomer_login') .
	" )";
$db->query($sql);
$sql = " DELETE FROM " . $ecs->table('vwaiting_queue') . 
	" WHERE salesperson_id not in ( " .
	"     SELECT id " .
	"       FROM " . $ecs->table('vsalesperson_login') .
	" )";
$db->query($sql);

$val = $json->encode($res);

die($val);

function is_blank($value) {
	return empty($value) && !is_numeric($value);
}
?>